from PyQt5.QtWidgets import *
from PyQt5 import QtGui
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import pymysql
sys.path.append(".")
sys.path.append("database/student")
sys.path.append("database/admin")
import interface.student.stu_query_history_course as sqhc
import database.student.student_ as student_

import database.admin.setting as setting
class Stu_QHC(QMainWindow,sqhc.Ui_Form):
    def __init__(self,name,id,grade):
        QMainWindow.__init__(self)
        sqhc.Ui_Form.__init__(self)
        self.setupUi(self)
        self.stu=student_.Student(id)
        self.setWindowTitle("学生已修课程及成绩查询")
        self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.sid=id
        self.id.setText(id)
        self.name.setText(name)
        self.grade.setText(grade)
        #print(id)
        self.table.setColumnCount(5)
        self.table.setHorizontalHeaderLabels(['课程号','课程名','修读时间','学分','成绩'])
        self.time.addItem('ALL')
        self.time.addItem('2019-2020秋学期')
        self.init_table()
        self.query.clicked.connect(self.find)
    def get_cj(self,re):
        all_c=0
        all_p=0
        print('getcj',re)
        for i in range(len(re)):
            all_c+=re[i][7]
            all_p+=re[i][6]*re[i][7]
        f_cj='%.2f'%(all_p/all_c)
        self.cj.setText(f_cj)
        return f_cj
    def init_table(self):
        self.find()
    def display_find_result(self, re):#'课程号','课程名','修读时间','成绩'
        self.table.setRowCount(0)
        row = self.table.rowCount()
        # self.table.setRowCount(row+1)
        re_len = len(re)
        # print(re[2])
        for l in range(re_len):
            row = self.table.rowCount()
            self.table.setRowCount(row + 1)
            self.table.setItem(row, 0, QTableWidgetItem(re[l][2]))
            self.table.setItem(row, 1, QTableWidgetItem(re[l][3]))
            self.table.setItem(row, 2, QTableWidgetItem(re[l][5]))
            self.table.setItem(row, 3, QTableWidgetItem(str(re[l][7])))
            self.table.setItem(row, 4, QTableWidgetItem(str(re[l][6])))
    def find(self):
        time = self.time.currentText()
        self.connect = pymysql.connect(**setting.config)
        self.cursor = self.connect.cursor()
        if(time!='ALL'):
            sql_ = "select * from history_sc where sid=%s and time like %s"
            arg = (self.sid,time)
            self.cursor.execute(sql_, arg)
        else:
            sql_ = "select * from history_sc where sid=%s"
            arg = (self.sid)
            self.cursor.execute(sql_, arg)
        re = self.cursor.fetchall()
        print('dsf',re)
        chengji=self.get_cj(re)
        print(chengji)
        self.cj.setText(chengji)
        self.display_find_result(re)
        self.cursor.close()
        self.connect.close()
        self.close()
        self.show()
if __name__=='__main__':
    app=QApplication(sys.argv)
    md=Stu_QHC('何凯明','2018300354','大三')
    md.show()
    sys.exit(app.exec_())